/**
 * @author 徐楠
 * @date 2022/1/11 14:06
 * @version 1.0
 */

package com.xunan.likou;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class IntersectionOfTwoArrays {
    public static void main(String[] args) {
        int[] nums1 = {4, 9, 5};
        int[] nums2 = {9, 4, 9, 8, 4};
        int[] intersection = intersection(nums1, nums2);
        System.out.println("--");
        for (int i : intersection) {
            System.out.println(i);
        }
        System.out.println("--");
    }

    public static int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> set = new HashSet<>();
        List<Integer> numList = new ArrayList<>();
        for (int i : nums1) {
            set.add(i);
        }
        System.out.println(set);
        System.out.println(set.contains(4));
        for (int i : nums2) {
            if (set.contains(i)) {
                if (!numList.contains(i)) {
                    numList.add(i);
                }
            }
        }
        int[] result = new int[numList.size()];
        for (int i = 0; i < numList.size(); i++) {
            result[i] = numList.get(i);
        }

        return result;
    }
}
